package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cheer.entity.ReferenceEntity;
import com.cheer.enums.RegisterStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 台账信息表
 * 
 * @author chufangzheng
 * @email 1280251739@qq.com
 * @date 2022-02-16 08:58:00
 */
@Mapper
public interface ReferenceDao extends BaseMapper<ReferenceEntity> {

    default ReferenceEntity getByCurrentYear(String yyyy, Long accountId){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReferenceEntity::getAccountId, accountId).eq(ReferenceEntity::getVintage, yyyy);
        ReferenceEntity referenceEntity = selectOne(queryWrapper);
        return referenceEntity;
    }

    default List<ReferenceEntity> selectByApplicantId(Long applicantId){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReferenceEntity::getApplicantId, applicantId);
        List<ReferenceEntity> lists = selectList(queryWrapper);
        return lists;
    }


    default ReferenceEntity selectByApplicantId(Long applicantId, String yyyy){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReferenceEntity::getApplicantId, applicantId).eq(ReferenceEntity::getVintage, yyyy);

        return selectOne(queryWrapper);
    }

    @Select("SELECT MAX(star_num) FROM `reference` where applicant_id = ${applicationId} and is_confer = 1")
    Integer getMaxStarNum(@Param("applicationId") Long applicationId);


    @Select("SELECT MAX(id) FROM `reference` where applicant_id = ${applicationId}")
    Long getMaxId(@Param("applicationId") Long applicationId);

    default List<ReferenceEntity> selectByRegisterStatus(RegisterStatusEnum provinceReview){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReferenceEntity::getRegisterStatus, provinceReview.getCode());

        return selectList(queryWrapper);
    }

    default List<ReferenceEntity> selectByRegisterStatusAndPost(RegisterStatusEnum provinceReview, Integer isPost){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReferenceEntity::getRegisterStatus, provinceReview.getCode())
                             .eq(ReferenceEntity::getIsPost, isPost);
        return selectList(queryWrapper);
    }


    default ReferenceEntity getByAppIdAndVintage(Long applicantId, String vintage){

        QueryWrapper<ReferenceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ReferenceEntity::getApplicantId, applicantId)
                .eq(ReferenceEntity::getVintage, vintage);

        return selectOne(queryWrapper);
    }





}
